package benny_driving_ser.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import benny_driving_ser.ObjectBean.PayCodeBean;
import benny_driving_ser.ObjectBean.TaximeterDataBean;


public class PayCodeUtil {
	
	public static String creatPayCode(String code){
		Random random = new Random();
		String code_new = MD5Util.md5(code);
		String result;
		String code1="",code2="",code3="",code4="";
		String sql_query_rule = "select code1,code2,code3,code4 from t_verify_rule where id = 1";
		Connection conn = DBUtil.getInstance().openConnection();
		try{
			conn.setAutoCommit(false);
			PreparedStatement pst = conn.prepareStatement(sql_query_rule);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				code1 = rs.getString("code1");
				code2 = rs.getString("code2");
				code3 = rs.getString("code3");
				code4 = rs.getString("code4");
			}
			rs.close();
			pst.close();
			conn.commit();
		}catch(SQLException e){
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer();
		sb.append(code_new.substring(code_new.length()-8,code_new.length()-4));
		String index = random.nextInt(4)+1+"";
		result = (sb.toString().charAt(0)+"")+index+(sb.toString().substring(1,4));
		switch(Integer.parseInt(index)){
		case 1:
			for(int i=0;i<4;i++){
				result += code_new.charAt(Integer.parseInt((code1.charAt(i)+"")));
			} break;
		case 2:
			for(int i=0;i<4;i++){
				result += code_new.charAt(Integer.parseInt((code2.charAt(i)+"")));
			} break;
		case 3:
			for(int i=0;i<4;i++){
				result += code_new.charAt(Integer.parseInt((code3.charAt(i)+"")));
			} break;
		case 4:
			for(int i=0;i<4;i++){
				result += code_new.charAt(Integer.parseInt((code4.charAt(i)+"")));
			} break;
		}
		DBUtil.getInstance().closeConnection(conn);
		return result;
	}
	
	public static boolean verify(String payCode,String full_payCode){
		
		String code1="",code2="",code3="",code4="",temp="",back="";
		String sql_query_rule = "select type,code1,code2,code3,code4 from t_verify_rule where id = 1";
		Connection conn = DBUtil.getInstance().openConnection();
		try{
			conn.setAutoCommit(false);
			PreparedStatement pst = conn.prepareStatement(sql_query_rule);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				code1 = rs.getString("code1");
				code2 = rs.getString("code2");
				code3 = rs.getString("code3");
				code4 = rs.getString("code4");
			}
			rs.close();
			pst.close();
			conn.commit();
			String index = payCode.charAt(1)+"";
			back = full_payCode.substring(full_payCode.length()-8,full_payCode.length()-4);
			back = (back.charAt(0)+"")+index+back.substring(1,4);
			switch(Integer.parseInt(index)){
			case 1:
				for(int i=0;i<4;i++){
					back += full_payCode.charAt(Integer.parseInt((code1.charAt(i))+""));
				} break;
			case 2:
				for(int i=0;i<4;i++){
					back += full_payCode.charAt(Integer.parseInt((code2.charAt(i))+""));
				} break;
			case 3:
				for(int i=0;i<4;i++){
					back += full_payCode.charAt(Integer.parseInt((code3.charAt(i))+""));
				} break;
			case 4:
				for(int i=0;i<4;i++){
					back += full_payCode.charAt(Integer.parseInt((code4.charAt(i))+""));
				} break;
			}
			if(payCode.equals(back)){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(conn);
		return  false;
	}

	
	
	public static int insertPayCode(String codesource,String paytype,String paycost,String wait,String times,	String dayOrNight,String isActivate){
		int num = 0;
		
		String sql_insert  = "insert into t_paycode(" +
				"codeSource," +//填写1
				"fullPayCode," +//生成2
				"payCode," +//生成3
				"payType," +//填写4
				"payCost," +//填写5
				"wait," +//填写6
				"addTime," +//生成7
				"used," +//默认0 7
				"times," +//默认为1 8
				"remainCost," +//默认为0  9
				"dayOrNight," +//默认为0  10
				"isActivate ) " +//默认为0 11
				"values(?,?,?,?,?,?,now(),?,?,(times*payCost),?,?);";
		
		Connection conn = DBUtil.getInstance().openConnection();
		try{
			conn.setAutoCommit(false);
			PreparedStatement pst_insert = conn.prepareStatement(sql_insert);
			pst_insert.setString(1, codesource);//1
			pst_insert.setString(2, MD5Util.md5(codesource));//2
			pst_insert.setString(3, PayCodeUtil.creatPayCode(MD5Util.md5(codesource)));//3
			pst_insert.setString(4, paytype);//4
			pst_insert.setString(5, paycost);//5
			pst_insert.setString(6, wait);//6
			pst_insert.setString(7, "0");//8
			if(Integer.parseInt(times)==1){
				pst_insert.setString(8, "1");//9
			}else{
				pst_insert.setString(8, times);//9
			}
			pst_insert.setString(9, dayOrNight);//10
			pst_insert.setString(10, isActivate);//11
			
			
			num = pst_insert.executeUpdate();
			pst_insert.close();
			conn.commit();
			return num;
			
		}catch(SQLException e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return 2;
		}finally{
			DBUtil.getInstance().closeConnection(conn);
		}
	}
	
	
	
	
	public static int calcCodeCost(List<PayCodeBean> list,TaximeterDataBean taximeterData){
		PayCodeBean payCodeBean = null;
		int payCodeCost = 0;
		if(list.size()==1){
			if(Integer.parseInt((payCodeBean=list.get(0)).getUsed())==0 || (Integer.parseInt(payCodeBean.getUsed())==1&&Integer.parseInt(payCodeBean.getTimes())>0)){
				if(Integer.parseInt(payCodeBean.getPayType())==1){
					try {
						payCodeCost = CalcDrivingCostUtil.getTaximeterPrivilege(Integer.parseInt(taximeterData.getPeriod()), Integer.parseInt(payCodeBean.getPayCost()));
						if(payCodeCost>Integer.parseInt(taximeterData.getTotal())){
							payCodeCost = Integer.parseInt(taximeterData.getTotal());
						}
						
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}
					return payCodeCost;
							
				}else if(Integer.parseInt(payCodeBean.getPayType())==2){
					
					if((Integer.parseInt(payCodeBean.getPayCost()))>=(Integer.parseInt(taximeterData.getTotal()))&&
							((Integer.parseInt(payCodeBean.getUsed())==0)||(Integer.parseInt(payCodeBean.getUsed())!=0&&Integer.parseInt(payCodeBean.getTimes())>0))&&
							Integer.parseInt(payCodeBean.getTimes())>0){
						return Integer.parseInt(taximeterData.getTotal());
					}else{
						return Integer.parseInt(payCodeBean.getPayCost());
					}
					
				}else if(Integer.parseInt(payCodeBean.getPayType())==3){
					int wait=0,sowt=0;
					return (wait=Integer.parseInt(payCodeBean.getPayCost()))>(sowt=Integer.parseInt(taximeterData.getSowt()))?
							Integer.parseInt(taximeterData.getSowp()):
								CalcDrivingCostUtil.getWaitCost(sowt-wait);
				}
				
			}else{
				return 0;
			}
		}else if(list.size()>1){
			return 0;
		}
		return 0;
	}
	
	public static void main(String[] args) {
		for(int i = 0;i<100;i++){
			String str = FormatUtil.getTimeFormat(System.currentTimeMillis());
			insertPayCode((System.currentTimeMillis()+1+""),"1","10","1","1","1","0");
			
		}
	}
}
